#!/usr/bin/env perl
################################################################# 
#  This is DroneBot ver1.1 writen by tech (techie@linuxirc.com) #
#  								#   	
#  Make sure you change the $mychan, $server, $port, and $oper	#
#  to fit your needs.						#
#  								#
#  In your sentinel.cfg file, place the channel(s) you need to 	#
#  monitor. 							#	
#  Then load DroneBot in your #Dronechannel			#
#  								#	
#  Commands: "Dronebot kills" Shows number of Drones killed	#
#								#
#								#
#								#
#								#
#################################################################

use IO::Socket;

$mynick = "DroneBot";
$myuser = "bot";
$mygecos = "Bot Killer";
$mychan = "#stat-drones";
$server = "irc.linuxirc.com";
$port = "6667";
$oper = "OPER nick password";
$logfile = "logger.txt"; 


# Set this to an empty string if you do not use NickServ
$nickserv = "NICKSERV :identify nick_password";

########################################################################
# Do not change anything below here unless you know what you are doing #
$sock = IO::Socket::INET->new(
	PeerAddr => $server, 
	PeerPort => $port, 
	Proto => 'tcp' ) or die "could not make the connection";
	
while($line = <$sock>){
	print $line;
	# WARNING:  This is Hybrid/Bahamut specific.  This will not work
	# on IRCnet.
	if($line =~ /checking ident/i){
		print $sock "NICK $mynick\r\nUSER $myuser 0 0 :$mygecos\r\n";
		last;
	}
	elsif ($line =~ /Doing reverse DNS lookup/) { # csircd
		print $sock "NICK $mynick\r\nUSER $myuser 0 0 :$mygecos\r\n";
		last;
	}
}

while($line = <$sock>){
	print $line;    
	#use next line if the server asks for a ping
	if($line =~ /^PING (.*)/){
		print $sock "PONG $1\r\n";
	}
	if($line =~ /(376|422)/i){
		print $sock "$nickserv\r\n";
		last;
	}
}

sleep 3;
print $sock "$oper\r\n";
#print $sock "OPERWALL :Dronebot is active\r\n"
print $sock "JOIN $mychan\r\n";
print $sock "PRIVMSG $mychan :Dronebot to the rescue!\r\n";
while ($line = <$sock>) {
	chomp($line);

	($command, $text) = split(/ :/, $line);  
	
	if ($command eq 'PING'){
		print $sock "PONG $text\r\n";
		next;
	}
	($nick,$type,$channel) = split(/ /, $line); 
	
	($nick,$userhost) = split(/!/, $nick); 
	
	$nick =~ s/://; #remove :'s

	if($channel eq $mychan){

		if ( $text =~ /DRONE MATCH:  (.*?)\!/) {
			print $sock "KILL $1 :DRONE\r\n";
		   open(LOG, "$logfile");
		   @file = <LOG>;
		   close(LOG);
		   $count = $file[0];
 		   $count++;
		   open(LOG,">$logfile");
		   print LOG "$count\n";
		 close(LOG);
		}

		print "<$nick> $text\n";

##basic commands#######################################

	    if($text =~ /$mynick kills/i){
		open(LOG,"<$logfile");
		@file = <LOG>;
		$count = $file[0];
		chomp($count);
		print $sock "PRIVMSG $mychan :I have killed \002$count\002 bots so far.\r\n";
		close(LOG);
	    }

                if($text =~ /$mynick die/i){
			print $sock "PRIVMSG $mychan :ok, im leaving now\r\n";
			sleep 1;
			die;
                }
	}

}


